import MySQLdb
import logging
class MySqlDb:
    def __init__(self):
        self.connect()
    def connect(self):
        self.m_conn = MySQLdb.connect(
            host='localhost',
            port = 3306,
            user='root',
            passwd='licheng',
            db ='waste',
            charset='utf8'
        )
        self.m_cur = self.m_conn.cursor()


    def close(self):
        self.m_cur.close()
        self.m_conn.close()

    def getAll(self,tablename):
        sql = "select * from %s" % tablename
        if tablename == "tb_zhihu":
            sql = "select * from tb_zhihu where verb='MEMBER_CREATE_ARTICLE'"
        ret = self.m_cur.execute(sql)
        return self.m_cur.fetchmany(ret)

    def update(self,tablenam,linkid,tag):
        sql = "update %s set tag='%s' where linkid='%s'" % (tablenam,tag,linkid)
        # print sql
        try:
            self.m_cur.execute(sql)
        except Exception as e:
            logging.warning("[-]Error "+e)
        else:
            self.m_conn.commit()
